//
//  GuideViewController.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/24.
//

#import "GuideViewController.h"

@interface GuideViewController ()

@property (nonatomic) NSTimer *timer;

@property (nonatomic) NSInteger netWorkType;

@property (nonatomic) UIImageView *iconV;
@property (nonatomic) UILabel *titleLab;
@property (nonatomic) UIButton *btn;

@end

@implementation GuideViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)judgeNet{
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if (status == 1 || status == 2){
            DLog(@"============the app have network========");
            self.netWorkType = 1;
        }
        else{
            DLog(@"============the app no network========");
            self.netWorkType = 0;
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [CreaditUtilsHelper hideLoadMessageBoxFromView:self.view];
        });
        
        if (self.netWorkType == 0) {
            [self initUI];
        }
        else{
            if (self.iconV) {
                [self.iconV removeFromSuperview];
            }
            if (self.titleLab) {
                [self.titleLab removeFromSuperview];
            }
            if (self.btn) {
                [self.btn removeFromSuperview];
            } 
        }
    }];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = RGB(4, 44, 8);
    
    [CreaditUtilsHelper boxShowLoadWithMessage:@"" onView:self.view withOffsetHeight:kNavigationBarHeight/2];
    
    [self judgeNet];
    
    self.timer = [NSTimer timerWithTimeInterval:1.f target:self selector:@selector(netWorkRequest) userInfo:nil repeats:YES];
        
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

- (void)netWorkRequest{
    if (self.netWorkType == 0) {
        DLog(@"check no network");
        [self judgeNet];
    }
    else{
        DLog(@"check have network");
        if (self.timer) {
            [self.timer invalidate];
            self.timer = nil;
            [[CreditCore globleCore] loadMainTabbarController];
        }
    }
}

- (void)initUI{
    
    if (self.iconV) {
        [self.iconV removeFromSuperview];
    }
    if (self.titleLab) {
        [self.titleLab removeFromSuperview];
    }
    if (self.btn) {
        [self.btn removeFromSuperview];
    } 
    
    UIImage *img = [UIImage imageNamed:@"icon_empty"];
    UIImageView *iconV = [[UIImageView alloc] init];
    [self.view addSubview:iconV];
    iconV.frame = CGRectMake((kScreenWidth - img.size.width)/2, kScreenHeight/2 - 180, img.size.width, img.size.height);
    iconV.image = img;
    self.iconV = iconV;

    UILabel *titleLab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:13] color:RGB(205, 220, 207)];
    titleLab.numberOfLines = 2;
    titleLab.textAlignment = 1;
    [self.view addSubview:titleLab];
    titleLab.frame = CGRectMake(40, iconV.bottom + 5, kScreenWidth - 80, 50);
    titleLab.text = @"Network error,please try again later or contact our customer service";
    self.titleLab = titleLab;
    
    UIImage *btnImg = [UIImage imageNamed:@"icon_emptyBtn"];
    self.btn = [[UIButton alloc] init];
    self.btn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:13];
    [self.btn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
    [self.btn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateSelected];
    [self.btn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateHighlighted];
    [self.view addSubview:self.btn];
    self.btn.frame = CGRectMake((kScreenWidth - btnImg.size.width)/2, self.titleLab.bottom + 5, btnImg.size.width, btnImg.size.height);
    [self.btn setBackgroundImage:btnImg forState:UIControlStateNormal];
    [self.btn setBackgroundImage:btnImg forState:UIControlStateSelected];
    [self.btn setBackgroundImage:btnImg forState:UIControlStateHighlighted];
    
    [self.btn setTitle:@"Try Again" forState:UIControlStateNormal];
    [self.btn setTitle:@"Try Again" forState:UIControlStateSelected];
    [self.btn setTitle:@"Try Again" forState:UIControlStateHighlighted];
    
    [self.btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)clickBtn{
    TipsAlertView *alertV = [[TipsAlertView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withTitleStr:@"To use Mabilis Money,you must enable network permission to use the App functions normally." withCancelStr:@"Stop" withSureStr:@"Turning On"];
    
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        window =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    [window addSubview:alertV];
    
    alertV.clickSureBlock = ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:^(BOOL success) {
                            
        }];
    };
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
